﻿using System;

namespace SmartPeople.Framework.SecuritySpace
{
    public sealed class Intializer
    {
        public void Intialize(ISecuritySpace securitySpace)
        {
            //TODO:check if null
            //create role administrator
            securitySpace.Roles.CreateRole(BuiltInRoles.Administrators, "Administrators of the application",
                                           "Administrators have complete and unrestricted access to the application");

            ISecurityRole adminRole = securitySpace.Roles.GetRole("Administrators");
            //set role administrator as system created by updating IsReadOnly to true
            adminRole.IsReadOnly = true;
            securitySpace.Roles.UpdateRole(adminRole);

            //create role users
            securitySpace.Roles.CreateRole(BuiltInRoles.Users, "Users of the application",
                                           "Users may have restricted access to prevent application-wide changes.");

            ISecurityRole userRole = securitySpace.Roles.GetRole("Users");
            //set role user as system created by updating IsReadOnly to true
            userRole.IsReadOnly = true;
            securitySpace.Roles.UpdateRole(userRole);

            //create role operators
            securitySpace.Roles.CreateRole("Operator", "Operators of the application",
                                           "Operators can have access of specific areas of the application");

            ISecurityRole operatorRole = securitySpace.Roles.GetRole("Operator");
            //set role operators as system created by updating IsReadOnly to true
            operatorRole.IsReadOnly = true;
            securitySpace.Roles.UpdateRole(operatorRole);

            //create user administrator
            securitySpace.Users.CreateUser("Administrator", "admin123", "administrator@acme.com",
                                           "What is your favourite color?", "Red", true, "Application Administrator");
            ISecurityUser user = securitySpace.Users.GetUser("Administrator");
            //set user administrator as system created by updating IsReadOnly to true
            user.IsReadOnly = true;
            securitySpace.Users.UpdateUser(user);

            //add administrator to role administrators
            string[] roles = {BuiltInRoles.Administrators};
            string[] users = {"Administrator"};
            securitySpace.Roles.AddUsersToRoles(users, roles);

            //create type folder


            //create type unknown file
            //ISecuritySecuredType securedFile = securitySpace.SecuredTypes.CreateType("unknownFileType");
            //securedFile.Actions.CreateAction(BuiltInActions.Read);
            //securedFile.Actions.CreateAction(BuiltInActions.Create);
            //securedFile.Actions.CreateAction(BuiltInActions.Modify);
            //securedFile.Actions.CreateAction(BuiltInActions.Delete);
            //securedFile.States.CreateState(BuiltInStates.New);
            //securedFile.States.CreateState(BuiltInStates.Normal);
            //securedFile.States.CreateState(BuiltInStates.Protected);
            //securitySpace.SecuredTypes.UpdateSecuredType(securedFile);

            securitySpace.GrantPermission(BuiltInRoles.Administrators, "/");

            
        }
    }
}